export default {
	data() {
		return {
			problem: '',
			answer: '',
			show: false,
			key: '',
			chat: [],
			type: '',
			loading:false,
		}
	},
	onShow() {
		this.init()

	},
	methods: {
		// 初始化
		init() {
			this.chat = uni.getStorageSync('historyProblem') || []
			this.key = uni.getStorageSync('key') || ''
			if(!this.chat || this.chat.length<=0){
				this.chat = [{
					name: 'chatgpt',
					message: "(GPT3.5模型)智能AI为您服务:\n1. 写代码、改BUG，写文案、续写小说\n2. 文案润色、翻译、写诗作词\n3. 扮演面试官、扮演书籍电影角色\n4. 生活百科，解答疑问\n\n例1: 鸟儿站在高压线上为什么没事？\n例2: 《狂飙》里的陈书婷是怎么死的？\n例3: 把下文翻译成英文:xxx\n例4: 帮我写篇日报，我只核对了些表格\n例5: 用王小波的风格写篇情书\n\n或者可以问我其它问题，越完整的描述，我越精确",
					status: 'init'
				}]
			}
		},
		// 复制
		copy(val) {
			uni.setClipboardData({
				data: this.answer || val,
				success: function() {
					uni.showToast({
						title: '复制成功',
						icon: 'none'
					})
				}
			});
		},
		// 切换key
		changeKey() {
			uni.setStorageSync('key', this.key)
			this.show = false
		},
		// 下个问题
		next() {
			uni.removeStorageSync('historyProblem');
			this.init()
			uni.showToast({
				title: '记忆已经清除！'
			})
		},
		// 滚动到最底部
		toBottom() {
			this.$nextTick(() => uni.pageScrollTo({
				scrollTop: 9999,
				duration: 300
			}))
		},
		// 获取问题答案
		async getAnswer() {
			if (this.problem == '清除记忆') {
				this.next()
				return
			}
			if (!this.problem || this.problem.match(/^\s*$/)) {
				uni.showToast({
					title: '不能发送空白消息',
					icon: 'none'
				})
				return
			}

			this.chat = [
				...this.chat,
				{
					name: 'my',
					message: this.problem,
					status: 'success'
				},
				// {
				// 	name: 'chatgpt',
				// 	message: '',
				// 	status: 'loading'
				// }
			]
			let index = this.chat.length - 1
			//当前发送的信息
			let current = this.problem
			//清空输入框
			this.problem = ''
			this.toBottom()
		
			this.loading = true
			//发起请求
			this.$myRequest({
				url:'/chat.sendMessage',
				data:{
					"prompt": current,
					"key": uni.getStorageSync('key') || '',
					"messages":this.handlePrompt(),
				},
				method:'post',
				title: '思考中'
			}).then(res=>{
				
				this.answer = res.data.result
				this.$set(this.chat, index, {
					name: 'chatgpt',
					message: res.success ==true ? res.data.result : res.message,
					status: res.success ==true ?'success':'error'
				})

				this.loading = false
				uni.setStorageSync('historyProblem', this.chat)
				this.toBottom()
			}).catch(err=>{
				this.answer = err
				this.$set(this.chat, index, {
					name: 'chatgpt',
					message: '很抱歉呀，出现了错误，请稍后再试或联系管理员',
					status: 'error'
				})
				
				this.loading = false
				uni.setStorageSync('historyProblem', this.chat)
				this.toBottom()
			});
			
			
		},
		handlePrompt() {
			let query = [{role:'assistant',content:'您是个得力助手'}]
			this.chat.forEach(item => {
				if (item.status == 'success') {
					switch (item.name) {
						case 'my':
							query.push({role:'user',content:item.message})
							break;
						case 'chatgpt':
							query.push({role:'assistant',content:item.message})
							break;
						default:
							break;
					}
				}

			})
			//以防提交数据过多，只取12行
			return query.slice(-12)
		}

	}
}
